package com.pengzy.comment.web.aop;

import com.pengzy.comment.core.enums.ThreadKeyEnum;
import com.pengzy.comment.mybatis.entity.SysLog;
import com.pengzy.comment.mybatis.mapper.SysLogMapper;
import com.pengzy.comment.web.thread.ThreadFactorys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;

/**
 * @author xiaozhi 2023/7/12
 */
@Slf4j
@Component
public class LogFilterExecutoServer {

    static ExecutorService executorService= null;
    @Resource
    private Environment environment;

    @Resource
    private SysLogMapper sysLogMapper;

    public void init(SysLog sysLog){
        if(executorService==null){
            executorService= ThreadFactorys.getCreateManagedExecutors(ThreadKeyEnum.LOG.getKey());
            synchronized (Object.class){
                if(executorService==null){
                    sysLogMapper.insert(sysLog);
                    return;
                }
            }
        }
        executorService.execute(()->sysLogMapper.insert(sysLog));
    }
}
